package com.yaoxiaowen.project2.test.recycler_view;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * YaoWen(43194) create at tongcheng work pc,
 * time:  2016/12/30 18:20  qq:2669932513
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder>{
    protected Context mContenxt;
    protected int mLayoutResId;

    protected List<T> mDatas = new ArrayList<>();
    protected  OnItemClickListner mOnItemClickListner;

    public CommonAdapter(Context mContenxt, @LayoutRes  int mLayoutResId) {
        this.mContenxt = mContenxt;
        this.mLayoutResId = mLayoutResId;
    }



    public void setOnItemClickListener(OnItemClickListner onItemClickListener){
        this.mOnItemClickListner = onItemClickListener;
    }

    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CommonViewHolder commonHolder = CommonViewHolder.get(mContenxt, null, parent, mLayoutResId, -1);
        setListener(parent, commonHolder, viewType);
        return commonHolder;
    }

    @Override
    public void onBindViewHolder(CommonViewHolder holder, int position) {
        holder.updatePosition(position);
        convert(holder, mDatas.get(position));
    }

    @Override
    public int getItemCount() {
        return mDatas==null? 0 : mDatas.size();
    }

    protected boolean isEnabled(int viewType){
        return true;
    }

    protected void setListener(final  ViewGroup parent, final CommonViewHolder viewHolder, int viewType){
        viewHolder.getContentView().setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (mOnItemClickListner != null){
                    int pos = getPosition(viewHolder);
                    mOnItemClickListner.onItemClick(parent, v, mDatas.get(pos), pos);
                }
            }
        });

        viewHolder.getContentView().setOnLongClickListener(new View.OnLongClickListener(){
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListner != null){
                    int pos = getPosition(viewHolder);
                    return mOnItemClickListner.onItemLongClick(parent, v, mDatas.get(pos), pos);
                }
                return false;
            }
        });
    }


    protected int getPosition(RecyclerView.ViewHolder viewHolder){
        return viewHolder.getAdapterPosition();
    }

    protected abstract void convert(CommonViewHolder holder, T data);


    public void resetData(List<T> datas){
        clearData();
        setData(datas);
    }

    private void clearData(){
        if (mDatas != null){
            mDatas.clear();
        }
    }

    private void setData(List<T> datas){
        if (mDatas!=null && datas!=null){
            mDatas.addAll(datas);
        }else {
            this.mDatas = datas;
        }
    }
}
